import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    public static void main(String [] args) {
        ArrayList<Student> list = new ArrayList<>();
        Scanner cin = new Scanner(System.in);
        while(true){
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选择：");
            String ops = cin.next();
            switch (ops){
                case "1" -> addStudent(list);
                case "2" -> deleteStudent(list);
                case "3" -> updateStudent(list);
                case "4" -> queryStudent(list);
                case "5" -> {
                    System.out.println("退出");
                    // break loop;
                    System.exit(0); // 停止虚拟机运行
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }

    public static void addStudent(ArrayList<Student> list){
        // 利用空参构造学生对象
        Student s = new Student();
        Scanner cin = new Scanner(System.in);
        String id = null;
        while(true){
            System.out.println("请输入学生id");
            id = cin.next();
            boolean flag = contains(list, id);
            if(flag){
                System.out.println("id已经存在，请重新输入");
            }else{
                s.setId(id);
                break;
            }
        }

        System.out.println("请输入学生的姓名");
        String name = cin.next();
        s.setUsername(name);

        System.out.println("请输入学生的年龄");
        int age = cin.nextInt();
        s.setAge(age);

        System.out.println("请输入学生的家庭住址");
        String address = cin.next();
        s.setAddress(address);

        list.add(s);

        System.out.println("学生信息添加成功");
    }

    public static void deleteStudent(ArrayList<Student> list){
        Scanner cin = new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = cin.next();
        int inx = getIndex(list, id);
        if(inx < 0){
            System.out.println("id不存在，删除失败");
            return;
        }
        list.remove(inx);
        System.out.println("id-" + id + "的学生删除成功");
    }

    public static void updateStudent(ArrayList<Student> list){
        Scanner cin = new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        String id = cin.next();
        int inx = getIndex(list, id);
//        System.out.println("cur time : " + inx);
        if(inx == -1){
            System.out.println("要修改的id-" + id + "不存在");
            return;
        }
        Student cur = list.get(inx);

        System.out.println("请输入要修改的学生姓名");
        String name = cin.next();
        cur.setUsername(name);

        System.out.println("请输入要修改的学生年龄");
        int age = cin.nextInt();
        cur.setAge(age);

        System.out.println("请输入要修改的学生家庭住址");
        String address = cin.next();
        cur.setAddress(address);

        list.set(inx, cur);

        System.out.println("学生信息修改成功");
    }

    public static void queryStudent(ArrayList<Student> list){
        if(list.isEmpty()){
            System.out.println("当前无学生信息，请添加后再查询");
            return;
        }
        System.out.println("id\t姓名\t年龄\t家庭住址");
        for(int i = 0; i < list.size(); i ++){
            Student cur = list.get(i);
            System.out.println(cur.getId() + '\n' + cur.getUsername() + "\t" + cur.getAge() + "\t" + cur.getAddress());
        }
    }

    public static boolean contains(ArrayList<Student> list, String id){
//        for(int i = 0; i < list.size(); i ++){
//            if(list.get(i).getId().equals(id)){
//                return true;
//            }
//        }
//        return false;
        return getIndex(list, id) >= 0;
    }

    public static int getIndex(ArrayList<Student> list, String id){
        for(int i = 0; i < list.size(); i ++){
            if(list.get(i).getId().equals(id)) return i;
        }
        return -1;
    }
}

